package com.mcxx.modules.temporary.fundpay.service.read;

import com.mcxx.common.exception.BizException;
import com.mcxx.modules.base.utils.QueryParamUtil;
import com.mcxx.modules.miniSalvation.fundpay.dto.CapitalFundPayDTO;
import com.mcxx.modules.miniSalvation.fundpay.dto.FundPayFamilyDTO;
import com.mcxx.modules.miniSalvation.fundpay.dto.FundPayFamilyExcelDTO;
import com.mcxx.modules.miniSalvation.fundpay.param.FundPayFamilyParam;
import com.mcxx.modules.temporary.fundpay.dao.read.TmpFundPayFamilyReadDao;
import com.mcxx.modules.temporary.fundpay.dao.read.TmpFundPayMemberReadDao;
import com.mcxx.modules.temporary.fundpay.dto.TmpFundPayDTO;
import com.mcxx.modules.temporary.fundpay.dto.TmpFundPayFamilyDTO;
import com.mcxx.modules.temporary.fundpay.dto.TmpFundPayFamilyExcelDTO;
import com.mcxx.modules.temporary.fundpay.entity.TmpFundPayEntity;
import com.mcxx.modules.temporary.fundpay.entity.TmpFundPayFamilyEntity;
import com.mcxx.modules.temporary.fundpay.param.TmpFundPayParam;
import com.mcxx.util.FailureCode;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.yinhai.ta404.core.restservice.resultbean.Page;
@Service
@TaTransactional(readOnly = true)
public class TmpFundPayFamilyReadService {

    @Autowired
    private TmpFundPayFamilyReadDao tmpFundPayFamilyReadDao;
    @Autowired
    private TmpFundPayMemberReadDao tmpFundPayMemberReadDao;


    public List<TmpFundPayFamilyEntity> listAll() {
    	return tmpFundPayFamilyReadDao.listAll();
    }


    public List<TmpFundPayFamilyEntity> getByFundId(String fundPayId){ return tmpFundPayFamilyReadDao.getByFundId(fundPayId); }


    public TmpFundPayFamilyEntity getById(String id) {
    	return tmpFundPayFamilyReadDao.getById(id);
    }


    public List<String> getNeedDelFamilyId(String fundPayId, Map<String, String> memberFamilyIdMap){

        Map<String, Integer> familyCntMemberMap = new HashMap<>();
        for(String familyId : memberFamilyIdMap.values()){
            familyCntMemberMap.put(familyId, familyCntMemberMap.containsKey(familyId) ? familyCntMemberMap.get(familyId) + 1 : 1);
        }


        List<FundPayFamilyDTO> familyCntMemberListDB = tmpFundPayMemberReadDao.cntMemberForFamilyByFundPayId(fundPayId, new ArrayList<>(familyCntMemberMap.keySet()));
        if(familyCntMemberListDB.size() <= 0){

            throw new BizException(FailureCode.ERR_10045);
        }
        Map<String, Integer> familyCntMemberMapDB = familyCntMemberListDB.stream().collect(Collectors.toMap(FundPayFamilyDTO::getFamilyId, FundPayFamilyDTO::getPersonNumber));


        List<String> needDelFamilyIdList = new ArrayList<>();
        for(String familyId : familyCntMemberMap.keySet()){
            if(familyCntMemberMap.get(familyId).equals(familyCntMemberMapDB.get(familyId))){

                needDelFamilyIdList.add(familyId);
            }
        }
        return needDelFamilyIdList;
    }


    public void cntFamilyByCapital(TmpFundPayDTO tmpFundPayDTO, UserAccountVo userAccountVo){
        TmpFundPayDTO tmpFundPayDTONew = new TmpFundPayDTO();
        BeanUtils.copyProperties(tmpFundPayDTO,tmpFundPayDTONew);

        QueryParamUtil.setBaseParamValue(tmpFundPayDTONew, userAccountVo);
        int rst = tmpFundPayFamilyReadDao.cntFamilyByCapital(tmpFundPayDTONew);
        if(rst <= 0){
            throw new BizException(FailureCode.ERR_20079);
        }
    }


    public List<TmpFundPayFamilyEntity> getFamilyByCreateCapital(TmpFundPayDTO tmpFundPayDTO){
        return tmpFundPayFamilyReadDao.getFamilyByCreateCapital(tmpFundPayDTO);
    }


    public int verifyNoCompleteNotice(String fundPayId){
        return tmpFundPayFamilyReadDao.getIdcardNoCompleteNotice(fundPayId);
    }



    public Page getNoSaveCapitalFamilyList(TmpFundPayParam tmpFundPayParam, PageParam pageParam){
        tmpFundPayFamilyReadDao.beginPager(pageParam);
        List<TmpFundPayFamilyDTO> list = tmpFundPayFamilyReadDao.getNoSaveCapitalFamilyList(tmpFundPayParam);
        return tmpFundPayFamilyReadDao.endPager(list);
    }


    public Page getCapitalFamilyList(TmpFundPayParam fundPayFamilyParam, PageParam pageParam, UserAccountVo userAccountVo){
        QueryParamUtil.setBaseParamValue(fundPayFamilyParam, userAccountVo);
        tmpFundPayFamilyReadDao.beginPager(pageParam);
        List<TmpFundPayFamilyDTO> list = tmpFundPayFamilyReadDao.getCapitalFamilyList(fundPayFamilyParam);
        return tmpFundPayFamilyReadDao.endPager(list);
    }



    public List<TmpFundPayFamilyEntity> getSaveCapitalFamilyByOrderId(List<String> orderIdList){
        return tmpFundPayFamilyReadDao.getSaveCapitalFamilyByOrderId(orderIdList);
    }


    public List<TmpFundPayFamilyExcelDTO>  exportCapitalObjectList(TmpFundPayParam param){
        return tmpFundPayFamilyReadDao.exportCapitalObjectList(param);
    }


    public int selectFundCountByOrderId(String orderId){
        return tmpFundPayFamilyReadDao.selectFundCountByOrderId(orderId);
    }

}
